//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.atjg.autocat.uvccamera.usb;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Build.VERSION;

import androidx.core.content.ContextCompat;

import com.atjg.autocat.uvccamera.callback.ConnectCallback;
import com.atjg.autocat.uvccamera.config.CameraConfig;
import com.atjg.autocat.uvccamera.utils.LogUtil;

import java.util.HashMap;
import java.util.Iterator;

public class UsbMonitor implements IMonitor {
    private static final String ACTION_USB_DEVICE_PERMISSION = "ACTION_USB_DEVICE_PERMISSION";
    private Context mContext;
    private UsbManager mUsbManager;
    private USBReceiver mUsbReceiver;
    private UsbController mUsbController;
    private ConnectCallback mConnectCallback;
    private CameraConfig mConfig;
    private PendingIntent pendingIntent;

    public UsbMonitor(Context context, CameraConfig config) {
        this.mContext = context;
        this.mConfig = config;
        this.mUsbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
    }

    public void registerReceiver() {
        LogUtil.i("registerReceiver");
        if (this.mUsbReceiver == null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
            filter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
            filter.addAction("ACTION_USB_DEVICE_PERMISSION");
            this.mUsbReceiver = new USBReceiver();
            ContextCompat.registerReceiver(this.mContext, this.mUsbReceiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED);
        }

    }

    public void unregisterReceiver() {
        LogUtil.i("unregisterReceiver");
        if (this.mUsbReceiver != null) {
            this.mContext.unregisterReceiver(this.mUsbReceiver);
            this.mUsbReceiver = null;
        }

    }

    public void checkDevice() {
        LogUtil.i("checkDevice");
        UsbDevice usbDevice = this.getUsbCameraDevice();
        if (this.isTargetDevice(usbDevice) && this.mConnectCallback != null) {
            this.mConnectCallback.onAttached(usbDevice);
        }

    }

    public void requestPermission(UsbDevice usbDevice) {
        LogUtil.i("requestPermission-->" + usbDevice);
        if (this.mUsbManager.hasPermission(usbDevice)) {
            if (this.mConnectCallback != null) {
                this.mConnectCallback.onGranted(usbDevice, true);
            }
        } else {
            if (VERSION.SDK_INT >= 31) {
                this.pendingIntent = PendingIntent.getBroadcast(this.mContext, 0, new Intent("ACTION_USB_DEVICE_PERMISSION"), PendingIntent.FLAG_IMMUTABLE);
            } else {
                this.pendingIntent = PendingIntent.getBroadcast(this.mContext, 0, new Intent("ACTION_USB_DEVICE_PERMISSION"), PendingIntent.FLAG_IMMUTABLE);
            }

            this.mUsbManager.requestPermission(usbDevice, this.pendingIntent);
        }

    }

    public void connectDevice(UsbDevice usbDevice) {
        LogUtil.i("connectDevice-->" + usbDevice);
        this.mUsbController = new UsbController(this.mUsbManager, usbDevice);
        if (this.mUsbController.open() != null && this.mConnectCallback != null) {
            this.mConnectCallback.onConnected(usbDevice);
        }

    }

    public void closeDevice() {
        LogUtil.i("closeDevice");
        if (this.mUsbController != null) {
            this.mUsbController.close();
            this.mUsbController = null;
        }

    }

    public UsbController getUsbController() {
        return this.mUsbController;
    }

    public UsbDeviceConnection getConnection() {
        return this.mUsbController != null ? this.mUsbController.getConnection() : null;
    }

    public void setConnectCallback(ConnectCallback callback) {
        this.mConnectCallback = callback;
    }

    public boolean hasUsbCamera() {
        return this.getUsbCameraDevice() != null;
    }

    public UsbDevice getUsbCameraDevice() {
        HashMap<String, UsbDevice> deviceMap = this.mUsbManager.getDeviceList();
        if (deviceMap != null) {
            Iterator var2 = deviceMap.values().iterator();

            while(var2.hasNext()) {
                UsbDevice usbDevice = (UsbDevice)var2.next();
                if (this.isUsbCamera(usbDevice)) {
                    return usbDevice;
                }
            }
        }

        return null;
    }

    public boolean isUsbCamera(UsbDevice usbDevice) {
        return usbDevice != null && 239 == usbDevice.getDeviceClass() && 2 == usbDevice.getDeviceSubclass();
    }

    public boolean isTargetDevice(UsbDevice usbDevice) {
        if (this.isUsbCamera(usbDevice) && this.mConfig != null && (this.mConfig.getProductId() == 0 || this.mConfig.getProductId() == usbDevice.getProductId()) && (this.mConfig.getVendorId() == 0 || this.mConfig.getVendorId() == usbDevice.getVendorId())) {
            LogUtil.i("Find target camera device");
            return true;
        } else {
            LogUtil.i("No target camera device");
            return false;
        }
    }

    private class USBReceiver extends BroadcastReceiver {
        private USBReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra("device");
            LogUtil.i("usbDevice-->" + usbDevice);
            if (UsbMonitor.this.isTargetDevice(usbDevice) && UsbMonitor.this.mConnectCallback != null) {
                switch (intent.getAction()) {
                    case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
                        LogUtil.i("onAttached");
                        UsbMonitor.this.mConnectCallback.onAttached(usbDevice);
                        break;
                    case "ACTION_USB_DEVICE_PERMISSION":
                        boolean granted = intent.getBooleanExtra("permission", false);
                        UsbMonitor.this.mConnectCallback.onGranted(usbDevice, granted);
                        LogUtil.i("onGranted-->" + granted);
                        break;
                    case "android.hardware.usb.action.USB_DEVICE_DETACHED":
                        LogUtil.i("onDetached");
                        UsbMonitor.this.mConnectCallback.onDetached(usbDevice);
                }

            }
        }
    }
}
